﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rad_Studio_Keygen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void test()
        {
            var r = DllInject.UnInject("radstudio_10_4_esd_101461a.tmp", "mOasisRuntime.dll");
            r = DllInject.Inject("radstudio_10_4_esd_101461a.tmp", DllInject.UnInjectDll);

            //var key = 0xE7F931C2;
            //var enc = RadKeygen.DeCryptoFile(@"C:\ProgramData\Embarcadero\.8221_52.1984087269307.slip", key);
            //File.WriteAllBytes(@"D:\lic.txt", enc);
            //RadKeygen.EnCryptoFile(enc, key, @"D:\abc.slip");
        }

        bool threadRunning = true;
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            foreach (var rv in RadStudioVersion.RadStudioVersionList)
            {
                comboBox1.Items.Add(rv.Key);
            }
            comboBox1.SelectedIndex = 0;

            textBox1.Text = RadKeygen.GenerateSerialNumber();
            textBox2.Text = RadKeygen.GetRegistrationCode();
            CheckForIllegalCrossThreadCalls = false;

            waitingPatchThread = new Thread(() =>
             {
                 var cv = GetCurrentVersion();
                 var RootDir = RadKeygen.GetRootDir(cv);
                 while (RootDir == null)
                 {
                     RootDir = RadKeygen.GetRootDir(cv);
                     Thread.Sleep(100);
                     if (!threadRunning)
                         return;
                 }
                 var FileName = "";
                 RadKeygen.PatchFile(cv, ref FileName);
             });
        }

        RadStudioVersion GetCurrentVersion()
        {
            return RadStudioVersion.RadStudioVersionList[comboBox1.Text];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
                textBox2.Text = RadKeygen.GetRegistrationCode();
            if (textBox2.Text.Trim() != "")
            {
                string FileName = "";
                if (RadKeygen.GenerateLicenseFile(textBox1.Text, textBox2.Text, GetCurrentVersion(), ref FileName))
                {
                    MessageBox.Show($"Slip文件保存成功！\n{FileName}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show($"保存Slip文件时发生错误！", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show($"请启动安装程序并到请求授权界面", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void textBox3_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
            }
        }

        private void textBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }


        Thread waitingPatchThread;
        private void button4_Click(object sender, EventArgs e)
        {
            button1_Click(null, null);
            if (textBox2.Text.Trim() != "")
            {
                var cv = GetCurrentVersion();
                RadKeygen.PatchmOasisRuntime(cv);
                var version = float.Parse(cv.BDSVersion);
                if (version >= 21.0)
                {
                    this.waitingPatchThread.Start();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string FileName = "";
            if (RadKeygen.PatchFile(GetCurrentVersion(), ref FileName))
            {
                MessageBox.Show($"补丁文件保存成功！\n{FileName}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show($"补丁文件失败！", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = RadKeygen.GenerateSerialNumber();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var sLineBreak = Environment.NewLine;
            var HowToUse = "怎么使用:" + sLineBreak;
            HowToUse += "1. 下载iso镜像文件:" + sLineBreak;
            HowToUse += "    {0}" + sLineBreak;
            HowToUse += "    MD5:{1}" + sLineBreak;
            HowToUse += "    版本:{2}" + sLineBreak;
            HowToUse += "2. 挂载 *.iso 镜像然后运行安装程序, 选择语言," + sLineBreak;
            HowToUse += "    设置同意许可协议的复选框, 单击 \"Next > \" " + sLineBreak;
            HowToUse += "    现在，您在“Input License”页面上..." + sLineBreak;
            HowToUse += "3. 单击 \" < Back\" 按钮 " + sLineBreak;
            HowToUse += "    现在使用注册机..." + sLineBreak;
            HowToUse += "4. 单击“生成”以获取新的序列号。 " + sLineBreak;
            HowToUse += "    !!! 请勿使用互联网上的任何其他序列号 !!! " + sLineBreak;
            HowToUse += "5. 现在，在安装页面中单击\"Next > \"，然后继续安装过程...  " + sLineBreak;
            HowToUse += "6. 安装完成后，单击“补丁”。 " + sLineBreak;
            HowToUse += "7. 开始 RAD Studio. " + sLineBreak;
            HowToUse += "====================================================" + sLineBreak;
            HowToUse += "Enjoy!!!";

            var cv = GetCurrentVersion();
            textBox3.Text = string.Format(HowToUse, cv.ISOUrl, cv.ISOMd5, cv.Ver);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            threadRunning = false;
        }
    }
}
